<%@ page pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.base.DatabaseObject"%>
<%@page import="com.base.Config"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="com.utils.LOBs"%>
<%@page import="java.sql.Clob"%>
<%@include file="inc-header.jsp"%><!-- MAIN CONTENT -->
<div class="box bottom10">
	<%@include file="inc-nav-header.jsp"%>
	<%	
	String sPCatId = Utils.getParam(request, "pcatid", "0");
	String sCatId = Utils.getParam(request, "catid", "0");
	String sId = Utils.getParam(request, "id", "0");
	String sPage = Utils.getParam(request, "page", "1");
	DatabaseObject category = contentDAO.getCategory(new BigDecimal(sPCatId));
	DatabaseObject category1 = contentDAO.getCategory(new BigDecimal(sCatId));
	DatabaseObject content = contentDAO.getContent(new BigDecimal(sId));
	
	String sClass1 = "class=\"active\"";
	String sClass2 = "class=\"active\"";
	if (category1 != null) sClass1 = "";
	
	int iCount = contentDAO.viewCount(new BigDecimal(sId));
	int iCountDownload = contentDAO.viewDownloadCount(new BigDecimal(sId));
	int icheckCount = contentDAO.checkCount(new BigDecimal(sId)); 	
	if(icheckCount <= 0)
	{
		boolean b = contentDAO.insertContentView(sId);
	}
	else
	{	
		iCount = contentDAO.viewCount(new BigDecimal(sId));
		boolean b = contentDAO.updateContentView(sId, iCount);
	}
	iCount = contentDAO.viewCount(new BigDecimal(sId));
	iCountDownload = contentDAO.viewDownloadCount(new BigDecimal(sId));	
	%>
	<div class="tab2">
		<%@include file="dichvu-adv.jsp" %> >>
		<a href="tone.jsp?act=dv"> Nhạc chuông </a>
		<%if (category != null) {%>
		>> <a href="tone-sub.jsp?act=dv&pcatid=<%=category.getAttribute().get("ID") %>" <%=sClass1 %>> <%=category.getAttribute().get("NAME") %></a>
		<%}%>
		<%if (category1 != null) {%>
		>> <a href="tone-sub.jsp?act=dv&pcatid=<%=sPCatId %>&catid=<%=category1.getAttribute().get("ID") %>" <%=sClass2 %>> <%=category1.getAttribute().get("NAME") %></a>
		<%}%>
	</div>
	<!--end .tab2-->
	<!-- BODY CONTENT -->
	<div class="box clearfix">
		<div class="table">
		    <table cellpadding="0" cellspacing="0" border="0" width="100%">
		        <tbody>
		            <tr>
		                <td width="4%"><a href="#"> <img src="<%=imagePath + content.getAttribute().get("preview_image")%>" alt="<%=content.getAttribute().get("name") %>" width="23" class="img-158-158"></a></td>
		                <td width="96%" class="text-left">
		                    <h2 class="clearfix"><a href="#" class="f1"> <strong> <%=content.getAttribute().get("name") %> </strong></a></h2>  
		                    <p><b>Giá tiền: <%=content.getAttribute().get("money") %> VNĐ</b></p> 
		                    <p class="bottom"> <strong class="l2"> Mã số: </strong> <%=content.getAttribute().get("id") %></p>
							<%
							dichden = getSevicesCode(Integer.parseInt(content.getAttribute().get("money").toString())); 
							String ua=request.getHeader("User-Agent").toLowerCase();
							if(ua != null){
								ua = ua.toUpperCase();
							}
							if(ua.indexOf("NOKIA") >= 0 || ua.indexOf("SAMSUNG") >= 0 || ua.indexOf("MOTOROLA") >= 0 || ua.indexOf("LG") >=0 || ua.indexOf("HTC") >=0 || ua.indexOf("GALAXY") >=0 || ua.indexOf("BLACKBERRY") >=0 || ua.indexOf("SONY") >=0){
					   		%>
					   		<p>Để <a href="sms:<%=dichden %>?body=<%=content.getAttribute().get("code") %>">Tải về <img src=<%=downloadIcon %>></a> soạn tin: <b class="show"><%=content.getAttribute().get("code")%></b> gửi <b class="show"><%=dichden %></b></p>
					   		<%} 
					   		else
					   		{%>
			   				<p>Để tải về soạn tin: <b class="show"><%=content.getAttribute().get("code")%></b> gửi <b class="show"><%=dichden %></b></p>
							<%}%>
							
				        </td>
		            </tr>
		        </tbody>
		    </table>
		</div><!--end .table-->
		<p> Số lượt xem: <%=iCount %> </p>
		<p> Số lượt tải: <%=iCountDownload %> </p>
		<div class="box-cont">
		    <h3> <b> Giới thiệu: </b></h3> 
		    <p class="MsoNormal" style="text-align: justify; line-height: normal;">
		        <span style="font-size: 10pt; font-family: Arial,sans-serif">
		            <%=content.getAttribute().get("detail") != null?LOBs.readClob((Clob)content.getAttribute().get("detail")).replaceAll("\n", "<br/>"):"" %>
		        </span>
		    </p>  
		</div><!--end .box-cont-->
		<div class="box clearfix">
		    <p align="center" class="bottom10">
		        <img src="<%=content.getAttribute().get("preview_image") %>" alt="">
		    </p>
		</div><!--end .box-->
		</div>
	<div class="box clearfix">
		<div class="title">
			<h3 class="bg">
				<strong>Cùng chuyên mục</strong>
			</h3>
		</div>
	</div>
	<div class="box clearfix">
<%
	int currPage = Integer.parseInt(sPage);
	int rowsPerPage = 5;
	if (!StringTool.isEmptyOrNul(sCatId)) {
		int totalRecs = contentDAO.countContentByCategory(new BigDecimal(sCatId));
		int numOfPages = (int)((totalRecs % rowsPerPage != 0) ? (totalRecs / rowsPerPage + 1)
				: (totalRecs / rowsPerPage));
		if (totalRecs <= rowsPerPage)
			numOfPages = 1;
		if (totalRecs == 0)
			numOfPages = 0;
		if (currPage > numOfPages)
			currPage = numOfPages;
		if (currPage <= 0)
			currPage = 1;
		long startPos = rowsPerPage * (currPage - 1) + 1;
		long stopPos = startPos + rowsPerPage - 1;
		if (stopPos > totalRecs)
			stopPos = totalRecs;
		long firstPage = 1; // The first page
		long lastPage = numOfPages; // The last page
		long prevPage = currPage - 1;
		if (prevPage < 1) {
			prevPage = 1;
		}
		long nextPage = currPage + 1;
		if (nextPage > numOfPages) {
			nextPage = numOfPages;
		}
	
		ArrayList<DatabaseObject> contentList = contentDAO.getContentListByCategory(new BigDecimal(sCatId), currPage, rowsPerPage);
		if (contentList != null && !contentList.isEmpty()) {%>
		<table cellpadding="0" cellspacing="0" border="1" width="100%">
		<%
			for (DatabaseObject ctent : contentList) {
				if (ctent.getAttribute().get("id").toString().equals(sId)) continue;
			%>
			<tr>
				<td width="4%" style="padding: 2px;">
			        <a href="tone-detail.jsp?act=dv&pcatid=<%=sPCatId %>&catid=<%=sCatId %>&id=<%=ctent.getAttribute().get("id") %>&page=<%=currPage %>"><img src="<%=imagePath + ctent.getAttribute().get("preview_image")%>" alt="<%=content.getAttribute().get("name") %>" class="img-45-30"></a>
			    </td>
			    <td width="96%" style="vertical-align: top; padding: 0px 2px 5px 4px;">
			        <p><a href="tone-detail.jsp?act=dv&pcatid=<%=sPCatId %>&catid=<%=sCatId %>&id=<%=ctent.getAttribute().get("id") %>&page=<%=currPage %>"><strong> <%=ctent.getAttribute().get("name") %> </strong></a></p>
							<%
							dichden = getSevicesCode(Integer.parseInt(ctent.getAttribute().get("money").toString())); 
							//String ua=request.getHeader("User-Agent").toLowerCase();
							if(ua != null){
								ua = ua.toUpperCase();
							}
							if(ua.indexOf("NOKIA") >= 0 || ua.indexOf("SAMSUNG") >= 0 || ua.indexOf("MOTOROLA") >= 0 || ua.indexOf("LG") >=0 || ua.indexOf("HTC") >=0 || ua.indexOf("GALAXY") >=0 || ua.indexOf("BLACKBERRY") >=0 || ua.indexOf("BLACKBERRY") >=0 || ua.indexOf("SONY") >=0){
					   		%>
					   		<p>Để <a href="sms:<%=dichden %>?body=<%=content.getAttribute().get("code") %>">Tải về <img src=<%=downloadIcon %>></a> soạn tin: <b class="show"><%=content.getAttribute().get("code")%></b> gửi <b class="show"><%=dichden %></b></p>
					   		<%} 
					   		else
					   		{%>
			   				<p>Để tải về soạn tin: <b class="show"><%=content.getAttribute().get("code")%></b> gửi <b class="show"><%=dichden %></b></p>
							<%}%>
							
			    </td>
			</tr>
			<%}%>
		</table>
		<%
		}%>
		<div align="center">
			<%
				long startPage = currPage - 3;
				if (startPage < 1) startPage = 1;
				
				long endPage = startPage + 6;
				if (endPage > numOfPages) {
					endPage = numOfPages;
				}
				if (startPage > 3 && endPage - startPage < 6) startPage = endPage - 6;
				if (startPage > 1) {
	        %>
			<a href="tone-sub.jsp?act=dv&pcatid=<%=sPCatId %>&catid=<%=sCatId %>&page=1">Đầu</a>
			<a href="tone-sub.jsp?act=dv&pcatid=<%=sPCatId %>&catid=<%=sCatId %>&page=<%=startPage-1 %>"> Trước </a>
			<%}
		        for (long i = startPage; i <= endPage; i++) {
		        	if (currPage == i) {
					out.print("<font style=\"font-weight:bold; font-size: 12px; color: #ff0000;\">" + i + "</font>");
				} else {
	         %>
			<a href="tone-sub.jsp?act=dv&pcatid=<%=sPCatId %>&catid=<%=sCatId %>&page=<%=i %>" style="font-size: 12px;"> <%=i%></a>
			<%  }
			}
	        if (endPage < numOfPages)
	        {
		    %>
			<a href="tone-sub.jsp?act=dv&pcatid=<%=sPCatId %>&catid=<%=sCatId %>&page=<%=endPage+1 %>" class="style_paging">Tiếp</a>
			<a href="tone-sub.jsp?act=dv&pcatid=<%=sPCatId %>&catid=<%=sCatId %>&page=<%=numOfPages %>" class="style_paging">Cuối</a>
			<%}%>
		</div>
		
		<%
	}
	%>
	</div>
	
	<div class="box clearfix">
		<div class="title">
			<h3 class="bg">
				<strong> Danh mục nhạc chuông</strong>
			</h3>
		</div>
		<!--end .title-->
		<ul class="list-dot"><%
		String catParentId = sPCatId;
		if (!StringTool.isEmptyOrNul(catParentId)) {
			ArrayList<DatabaseObject> categoryList = contentDAO.getCategory(catParentId, null);
			for (DatabaseObject cat : categoryList) {%>
			<li>
				<img src="images/icon-list.png" alt="<%=cat.getAttribute().get("NAME") %>">
				<a href="tone-sub.jsp?act=dv&pcatid=<%=sPCatId %>&catid=<%=cat.getAttribute().get("ID") %>"> <%=cat.getAttribute().get("NAME") %> </a>
			</li>
		<%}}%>
		</ul>
	</div>
	<!-- END BODY CONTENT -->
</div>
<!--end .box bottom10-->
<%@include file="inc-nav-bottom.jsp"%>
<!-- END MAIN CONTENT -->
<%@include file="inc-footter.jsp"%>
